package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.Act;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.ActCategoryMapper;
import com.ruoyi.system.domain.ActCategory;
import com.ruoyi.system.service.IActCategoryService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * 作品类别Service业务层处理
 *
 * @author ruoyi
 * @date 2021-11-27
 */
@Service("actCategoryService")
public class ActCategoryServiceImpl implements IActCategoryService
{
    @Autowired
    private ActCategoryMapper actCategoryMapper;

    /**
     * 查询作品类别
     *
     * @param id 作品类别主键
     * @return 作品类别
     */
    @Override
    public ActCategory selectActCategoryById(Long id)
    {
        return actCategoryMapper.selectActCategoryById(id);
    }

    /**
     * 查询作品类别列表
     *
     * @param actCategory 作品类别
     * @return 作品类别
     */
    @Override
    public List<ActCategory> selectActCategoryList(ActCategory actCategory)
    {
        return actCategoryMapper.selectActCategoryList(actCategory);
    }

    /**
     * 查询级别1的所有类别
     * @return
     */
    @Override
    public List<ActCategory> selectActCategoryListLevel1(){
        return actCategoryMapper.selectActCategoryListLevel1();
    }

    /**
     * 查询所有作品类别
     *
     * @return 作品类别
     */
    @Override
    public List<ActCategory> selectAll()
    {
        return actCategoryMapper.selectActCategoryList(null);
    }

    /**
     * 新增作品类别
     *
     * @param actCategory 作品类别
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertActCategory(ActCategory actCategory)
    {
        setCategoryParentIdAndAncestors(actCategory);
        actCategory.setCreateTime(DateUtils.getNowDate());
        return actCategoryMapper.insertActCategory(actCategory);
    }

    /**
     * 修改作品类别
     *
     * @param actCategory 作品类别
     * @return 结果
     */
    @Override
    public int updateActCategory(ActCategory actCategory)
    {
        setCategoryParentIdAndAncestors(actCategory);
        actCategory.setUpdateTime(DateUtils.getNowDate());
        return actCategoryMapper.updateActCategory(actCategory);
    }

    /**
     * 设置类别的ParentId和 Ancestors
     * @param actCategory
     */
    private void setCategoryParentIdAndAncestors(ActCategory actCategory){
        String ancestors = "";
        if(actCategory.getParentId() == null || actCategory.getParentId().intValue() == 0){
            actCategory.setParentId(0L);
            ancestors = "0";
        }else{
            ActCategory parentCategory = actCategoryMapper.selectActCategoryById(actCategory.getParentId());
            ancestors = parentCategory.getAncestors()+ "," + parentCategory.getId();
        }
        actCategory.setAncestors(ancestors);
    }

    /**
     * 批量删除作品类别
     *
     * @param ids 需要删除的作品类别主键
     * @return 结果
     */
    @Override
    public int deleteActCategoryByIds(String ids)
    {
        return actCategoryMapper.deleteActCategoryByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除作品类别信息
     *
     * @param id 作品类别主键
     * @return 结果
     */
    @Override
    public int deleteActCategoryById(Long id)
    {
        return actCategoryMapper.deleteActCategoryById(id);
    }

    /**
     * 查询类别树
     * @return
     */
    @Override
    public List<ActCategory> findWholeTree(){
        List<ActCategory> list = actCategoryMapper.selectActCategoryListLevel1();
        if(list != null && list.size() >0){
            for(ActCategory actCategory: list){
                setActCategoryChildred(actCategory);
            }
        }
        return list;
    }

    /**
     * 查找所有叶子节点
     * @return
     */
    @Override
    public List<ActCategory> findTreeLeaf(){
        List<ActCategory> list = actCategoryMapper.selectActCategoryListLevel1();
        List<ActCategory> result = new ArrayList<>();
        if(list != null && list.size() > 0){
            for(ActCategory actCategory : list){
                findLeafNode(actCategory,result);
            }
        }
        return result;
    }

    /**
     * 查询有叶子节点的父节点集合
     * @return
     */
    @Override
    public List<ActCategory> findTreeParentNodes(){
        List<ActCategory> list = actCategoryMapper.selectActCategoryListLevel1();
        List<ActCategory> result = new ArrayList<>();
        if(list != null && list.size() >0){
            for(ActCategory actCategory: list){
                List<ActCategory> childrens = actCategoryMapper.selectListByParentId(actCategory.getId());
                if(childrens != null && childrens.size() > 0){
                    result.add(actCategory);
                }
            }
        }
        return result;
    }

    private void findLeafNode(ActCategory parentNode,List<ActCategory> result){
        List<ActCategory> childNodes = actCategoryMapper.selectListByParentId(parentNode.getId());
        if(childNodes == null || childNodes.size() == 0){
            result.add(parentNode);
        }else{
            for (ActCategory category : childNodes){
                findLeafNode(category,result);
            }
        }
    }


    private void setActCategoryChildred(ActCategory actCategory){
        List<ActCategory> childrens = actCategoryMapper.selectListByParentId(actCategory.getId());
        if(childrens != null && childrens.size() >0){
            for(ActCategory child : childrens){
                setActCategoryChildred(child);
            }
            actCategory.setChildren(childrens);
        }
    }
}
